'use strict';

import "../lib/layer/layer.js";
import { addEvent } from "../lib/tools.js";
import { login, reg } from "./http/interface.js";

const check = {
    // 添加 class 类名
    addClass(elem, className) {
        if (!elem || !className) return;
        elem.classList.add('has-error');
    },

    // 移除 class 类名
    removeClass(elem, className) {
        if (!elem || !className) return;
        elem.classList.remove('has-error');
    },

    isEmpty(elem, value, msg = '必填项不能为空') {
        var parentElem = elem.parentNode,
            nextElem = elem.nextElementSibling;

        if (value === '' || value == null) {
            elem.focus();
            layer.msg(msg, {
                icon: 2,
            });
            this.addClass(parentElem, 'has-error');
            return true;
        }else{
            this.removeClass(parentElem, 'has-error');
        }
    },
};

(function() {
    var form = document.getElementsByClassName('form-submit')[0];

    document.getElementsByName('username')[0].focus();

    if (form) {
        addEvent(form, 'submit', function() {
            var success = function(result) {
                    if (result.code !== 0) {
                        layer.msg(result.msg, {
                            icon: 2,
                        });
                    }else{
                        layer.msg(result.msg, {
                            icon: 1,
                        }, function() {
                            if (result.data) {
                                window.location.href = result.data;
                            }else{
                                location.reload();
                            }
                        });
                    }
                },
                error = function (result) {
                    layer.msg(result, {
                        icon: 2,
                    });
                };
            var username = document.getElementsByName('username')[0],
                passwd = document.getElementsByName('passwd')[0];

            if (check.isEmpty(username, username.value, '用户名不能为空')) return;
            if (check.isEmpty(passwd, passwd.value, '密码不能为空')) return;

            var data = {
                username: username.value,
                passwd: passwd.value
            };

            var reg_form = document.getElementsByClassName('reg-form')[0];
            if (reg_form) {
                var repasswd = document.getElementsByName('repasswd')[0],
                    sex = document.getElementsByName('sex'),
                    sex_val = 0,
                    nickname = document.getElementsByName('nickname')[0];
                if (check.isEmpty(repasswd, repasswd.value, '确认密码不能为空')) return;
                if (check.isEmpty(nickname, nickname.value, '昵称不能为空')) return;

                for (var i=0; i<sex.length; i++)
                    if (sex[i].checked)
                        sex_val = sex[i].value;

                data['repasswd'] = repasswd.value;
                data['sex'] = sex_val;
                data['nickname'] = nickname.value;
                return reg(data, success, error);
            }

            login(data, success, error);
        });
    }
}());
